rm(list=ls())

# Start the clock!
processingTime <- proc.time()

setwd('~/Dropbox/Collective Action/software')
source("gameClassDefs-140616.R")
source("repeatGamePlot.R")
source("templateMgr-BDT-140616.R")

library(multicore)


# choose agents and game payoffs from template file 'templateMgr-BDT-140616.R'
# to add new agent or game types, edit templateMgr-BDT.R and run:
updateGameTemplates(getwd())
load("gameTemplates-BDT.Rimage")

# setwd('~/Dropbox/Collective Action/software')
set.seed(2045635090, kind="Mersenne-Twister")

# function to run one game session
# reports fraction of time that each player cooperated
runGame <- function(iter, payoffList, nPlays, agentClass, agentProto1, agentProto2) {
	p1 <- new(agentClass, agentProto1)
	p2 <- new(agentClass, agentProto2)
	
	game1 <- new("normalGame", payoffs=payoffList, players=list(p1,p2))
	
	repeatPlay(game1, nPlays)
}

nSim <- as.integer(1000)  # change number of simulations
nPlay <- as.integer(1000) # change number of periods
abarType <- "bm"

# the following are payoff matrices from 'templateMgr-BDT-140616.R'.

BDT.V1 <- gameTemplate$BDT.V6a
BDT.V2 <- gameTemplate$BDT.V6c
BDT.V3 <- gameTemplate$BDT.V3
BDT.V4 <- gameTemplate$BDT.V7c
BDT.V5 <- gameTemplate$BDT.V8a

for (gameName in c("BDT.V1","BDT.V2","BDT.V3","BDT.V4","BDT.V5")){
	pL <- get(gameName)
	pM <- matrix(data=unlist(pL), nrow=(length(unlist(pL)) / length(pL)),ncol=length(pL))

	# to change behavior rule, replace instances of agentTemplate$xxx with relevant model
	# from 'templateMgr-BDT-140616.R'
	
	# asocial version
	
	cat(paste("Running Sims:",gameName,", asocial...\n",sep=" "))
	aC <- "abarAgent"			# switch to "abarAgent" to use non-reference group player types, "socialAgent" for social types
	ap1 <- createAgentProto(type=aC, abar=abarType, id = as.integer(1), nActions=as.integer(2), paramsList=agentTemplate$bmPropInertiaSensAsocial)
	ap2 <- createAgentProto(type=aC, abar=abarType, id = as.integer(2), nActions=as.integer(2), paramsList=agentTemplate$bmPropInertiaSensAsocial)
	allOutcomes <- do.call(rbind, mclapply(1:nSim, FUN=runGame, payoffList=pL, nPlays=nPlay, agentClass=aC, agentProto1=ap1, agentProto2=ap2))
	rgPlotWithStates(allOutcomes, dims=c(2,2), filename=paste(gameName,"-asocial.pdf",sep=""))
	rm(ap1, ap2, allOutcomes)
	
	# social version

	cat(paste("Running Sims:",gameName,", social...\n",sep=" "))
	aC <- "socialAgent"
	ap1 <- createAgentProto(type=aC, abar=abarType, id = as.integer(1), nActions=as.integer(2), paramsList=agentTemplate$bmPropInertiaSens, refGrp=as.integer(2))
	ap2 <- createAgentProto(type=aC, abar=abarType, id = as.integer(2), nActions=as.integer(2), paramsList=agentTemplate$bmPropInertiaSens, refGrp=as.integer(1))
	allOutcomes <- do.call(rbind, mclapply(1:nSim, FUN=runGame, payoffList=pL, nPlays=nPlay, agentClass=aC, agentProto1=ap1, agentProto2=ap2))
	rgPlotWithPayoffs(allOutcomes, dims=c(2,2), payoffs=pM, filename=paste(gameName,"-social.pdf",sep=""))
	rm(ap1, ap2, allOutcomes)

}

# Stop the clock
proc.time() - processingTime
